function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
$(function () {
    //注意：选项卡 依赖 element 模块，否则无法进行功能性操作
    layui.use(['element', 'form'], function () {
        var element = layui.element;
        if (getQueryString('tabIndex')) {
            vm.nav_tabs = getQueryString('tabIndex');
            element.tabChange('docDemoTabBrief', vm.nav_tabs);
           // console.log(getQueryString('tabIndex'));
        }
        showTable();
        element.on('tab(docDemoTabBrief)', function (data) {
            vm.nav_tabs = data.index == 0 ? 2 : 1;
            //console.log(vm.nav_tabs);
            vm.query();
        });


        //…
    });

 function showTable() {
      $("#jqGrid").jqGrid({
        url: baseURL + 'sys/schedule/list',
        datatype: "json",
        postData: {
            'jobType': vm.nav_tabs
        },
        colModel: [
            {label: '任务ID', name: 'jobId', width: 60, key: true},
            {label: 'bean名称', name: 'beanName', width: 100},
            {label: '方法名称', name: 'methodName', width: 100},
            {
                label: '参数', name: 'params', width: 100, formatter: function (value, options, row) {
                    return '<div class="texts">' + (value == null ? '' : value) + '</div>'
                }
            },
            {label: 'cron表达式 ', name: 'cronExpression', width: 100},
            {
                label: '备注 ', name: 'remark', width: 100, formatter: function (value, options, row) {
                    if (value) {
                        return '<div class="texts">' + value + '</div>'
                    } else {
                        return ''
                    }

                }
            },

            {
                label: '状态', name: 'status', width: 60, formatter: function (value, options, row) {
                    return value === 0 ?
                        '<span class="label label-success">正常</span>' :
                        '<span class="label label-danger">暂停</span>';
                }
            },
            {
                label: '日志', name: 'jobId', width: 60, formatter: function (value, options, row) {
                    return '<a class="btn btn-warning" href="schedule_log.html?id=' + value + '&tabIndex=' + vm.nav_tabs + '">查看日志</a>'
                }
            }
        ],
        ExpandColClick: true,
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"

        },
        gridComplete: function () {
            $("#schedule_search").attr("disabled", false);
            //隐藏grid底部滚动条
            // $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
};

});
var types = 2;
var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			beanName: null,
            remark:null
		},
		isDel:false,
		showList: true,
		title: null,
		isReadOnly:null,
		schedule: {},
        nav_tabs:2,
        istype:2,
        isstart:2
	},

	methods: {

		query: function () {
            vm.$refs.schedule_search.disabled = true;
            //console.log(this.nav_tabs);
			vm.reload(1);
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
            vm.isReadOnly = null;
			vm.schedule = {};
            $('#radios :radio').eq(0).prop('checked','checked')
            $('#radios :radio').eq(1).removeAttr('checked')
            $('#radios2 :radio').eq(0).prop('checked','checked')
            $('#radios2 :radio').eq(1).removeAttr('checked')
		},
        copy:function(){
            var jobId = getSelectedRow();
            if(jobId == null){
                return ;
            }
            $.get(baseURL + "sys/schedule/info/"+jobId, function(r){
                vm.showList = false;
                vm.title = "新增";
                vm.schedule = r.schedule;
                vm.isReadOnly = null;
                vm.schedule.jobId = null;
                var i = r.schedule.jobType==2?0:1;
                $('#radios :radio').removeAttr('checked')
                $('#radios :radio').eq(i).prop('checked','checked')
                $('#radios2 :radio').eq(0).prop('checked','checked')
                $('#radios2 :radio').eq(1).removeAttr('checked')



            });
            console.log(jobId,'ssssssssss')
        },
		update: function () {

			var jobId = getSelectedRow();
			if(jobId == null){
				return ;
			}

			$.get(baseURL + "sys/schedule/info/"+jobId, function(r){
				vm.showList = false;
                vm.title = "修改";
				vm.schedule = r.schedule;
				vm.isReadOnly = "readonly";
                var i = r.schedule.jobType==2?0:1;
                $('#radios :radio').removeAttr('checked')
                $('#radios :radio').eq(i).prop('checked','checked')




			});
		},
		saveOrUpdate: function (event) {
			var url = vm.schedule.jobId == null ? "sys/schedule/save" : "sys/schedule/update";
			var btn  = vm.$refs.schedule_submit;
			//console.log(btn);
			btn.disabled = true;
            vm.schedule.jobType = $('input[name="type"]:checked').val();
            vm.schedule.firstStart = $('input[name="start"]:checked').val()==2?true:false;
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.schedule),
			    success: function(r){
                    btn.disabled = false;
                    if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var jobIds = getSelectedRows();
			if(jobIds == null){
				return ;
			}
			confirm('确定要删除选中的'+jobIds.length+'条记录？', function(){

                if(!vm.isDel){
                    vm.isDel = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/schedule/delete",
                        contentType: "application/json",
                        data: JSON.stringify(jobIds),
                        success: function(r){
                        	vm.isDel = false;
                            if(r.code == 0){
                                alert('操作成功', function(index){
                                    vm.reload();
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
				}else{
                    alert("操作过于频繁!")
                }

			});
		},
		pause: function (event) {
			var jobIds = getSelectedRows();
			if(jobIds == null){
				return ;
			}

			confirm('确定要暂停选中的记录？', function(){
                if(!vm.isDel) {
                    vm.isDel = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/schedule/pause",
                        contentType: "application/json",
                        data: JSON.stringify(jobIds),
                        success: function (r) {
                            vm.isDel = false;
                            if (r.code == 0) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }else{
                    alert("操作过于频繁!")
                }
			});
		},
		resume: function (event) {
			var jobIds = getSelectedRows();
			if(jobIds == null){
				return ;
			}

			confirm('确定要恢复选中的记录？', function(){
                if(!vm.isDel) {
                    vm.isDel = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/schedule/resume",
                        contentType: "application/json",
                        data: JSON.stringify(jobIds),
                        success: function (r) {
                            vm.isDel = false;
                            if (r.code == 0) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }else{
                    alert("操作过于频繁!")
                }
			});
		},
		runOnce: function (event) {
			var jobIds = getSelectedRows();
			if(jobIds == null){
				return ;
			}

			confirm('确定要立即执行选中的记录？', function() {
                if(!vm.isDel){
                	vm.isDel = true;

                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/schedule/run",
                    contentType: "application/json",
                    data: JSON.stringify(jobIds),
                    success: function (r) {
                        vm.isDel = false;
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }else{
                   alert("操作过于频繁!")
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
            //console.log(vm.nav_tabs);
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{
                    'beanName': vm.q.beanName,
                    'jobType':vm.nav_tabs,
                    'remark':vm.q.remark
                },
                page:event?1:page
            }).trigger("reloadGrid");
		}
	}
});